package visual;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;

import service.data.SuggestDataService;
import service.network.NetworkFilesystemService;

import materials.network.NetworkInterface;

public class SuggestDataFrame extends JFrame{

	JTextField _input_textfield = new JTextField();
	JTextField _output_textfield = new JTextField();

	SuggestDataService _sds = new SuggestDataService(null);
	
	public static void main(String[] args){
		new SuggestDataFrame();
	}
	
	public SuggestDataFrame(){
		super();
		initGUI();
		vpt.setVisible(true);
	}

	private void initGUI() {
		this.setBounds(100, 100, 600, 250);
		getContentPane().setLayout(null);
		{
			JButton _calculate = new JButton("Run");
			_calculate.setBounds(10, 40, 150, 25);
			_calculate.setVisible(true);
			_calculate.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					run();
				}
			});
			getContentPane().add(_calculate);
		}
		{
			JButton _loadnet = new JButton("LoadNet");
			_loadnet.setBounds(10, 80, 150, 25);
			_loadnet.setVisible(true);
			_loadnet.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					loadNetwork();
				}
			});
			getContentPane().add(_loadnet);
		}
		{
			_input_textfield.setBounds(10, 10, 500, 25);
			_input_textfield.setVisible(true);
			getContentPane().add(_input_textfield);
		}
		{
			_output_textfield.setBounds(10, 120, 500, 25);
			_output_textfield.setVisible(true);
			getContentPane().add(_output_textfield);
		}
		
		this.setVisible(true);
		this.setAlwaysOnTop(true);
		//(0,0,0,0,0,0,0,0,0,0,0,100,-42,0,0,0,0,0,0,0,0,0,0,0,0)
	}
	
	// (12,1,121,ee)
	
	protected void calculate(){
		String s = _input_textfield.getText();
		s = s.substring(1,s.length()-1);
		String[] arr = s.split(",");
		double[] inp = new double[arr.length];
		for(int i = 0; i < arr.length; i++){
			inp[i] = Double.parseDouble(arr[i]);
		}
		double[] resdata = _sds.testData(inp);
		double m = 0;
		int id = 0;
		for(int  z  = 0; z < resdata.length;z++){
			if( resdata[z] > m){
				m = resdata[z];
				id = z;
			}
		}
		System.out.println("ID:" + id);
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		for(int i = 0; i < resdata.length; i++){
			sb.append( resdata[i] + ",");
		}
		String j = sb.toString().substring(0,sb.toString().length()-1) + ")";
		_output_textfield.setText(j);
	}
	
	
	protected void run(){
		while(true){
			String h = "(" + ((int)((Math.random() * 200) - 100)) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			"-42," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + "," +
			(int)((Math.random() * 200) - 100) + ")";
			String s = h;
			s = s.substring(1,s.length()-1);
			String[] arr = s.split(",");
			double[] inp = new double[arr.length];
			for(int i = 0; i < arr.length; i++){
				inp[i] = Double.parseDouble(arr[i]);
			}
			double[] resdata = _sds.testData(inp);
			double m = 0;
			int id = 0;
			for(int  z  = 0; z < resdata.length;z++){
				if( resdata[z] > m){
					m = resdata[z];
					id = z;
				}
			}
			
			vpt.addData(id);
		}
	}
	
	ValuePerTime vpt = new ValuePerTime();
	
	protected void calculateNet() {
		String s = _input_textfield.getText();
		s = s.substring(1,s.length()-1);
		String[] arr = s.split(",");
		double[] inp = new double[arr.length];
		
		for(int i = 0; i < arr.length; i++){
			inp[i] = Double.parseDouble(arr[i]);
		}
		double[] resdata = _sds.testData(inp);
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		for(int i = 0; i < resdata.length; i++){
			sb.append( (int)(resdata[i]*1000) + ",");
		}
		String j = sb.toString().substring(0,sb.toString().length()-1) + ")";
		_output_textfield.setText(j); // (0,0,0,0,0,0,0,0,0,0,0,0,-42,0,0,0,0,0,0,0,0,0,0,0,0)
	}

	public void loadNetwork(){
		NetworkFilesystemService nfss = new NetworkFilesystemService();
		JFileChooser jf = new JFileChooser();
		jf.showOpenDialog(this);
		File f = jf.getSelectedFile();
		NetworkInterface ni = nfss.loadNetwork(f);
		_sds.setNet(ni);
	}
	
	
}
